/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.dli;

import java.util.LinkedList;

public class Type {

    private LinkedList<Adjective> adjectiveList = new LinkedList<Adjective>();
	private ItemId itemId = null;	
    private LinkedList<Term> termList = new LinkedList<Term>();
    private boolean withType = true;

	public static Type newInstance(String string) throws DliParseException {
		Type type = new Type();
		int position = string.indexOf("(");
		if (string.startsWith("$type(")){
			string = string.substring(position+1, string.length()-1);
		} else {
			type.withType = false;
		}
		int positionMode = string.indexOf(":mode ");
		int positionStruct = string.indexOf(":struct ");
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma < positionMode || positionComma < positionStruct) {
			while (positionComma != -1) {
				type.adjectiveList.add(Adjective.newInstance(string.substring(0, positionComma)));
				string = string.substring(positionComma+1, string.length());
				positionComma = Utility.searchSeparationComma(string);
			} 
		}
		position = string.indexOf("(");
		if (position == -1) {
			type.itemId = ItemId.newInstance(string);
		} else
		{
			type.itemId = ItemId.newInstance(string.substring(0,position));
			string = string.substring(position+1, string.length()-1);
			positionComma = Utility.searchSeparationComma(string);
			while (positionComma != -1) {
				type.termList.add(Term.newInstance(string.substring(0, positionComma)));
				string = string.substring(positionComma+1, string.length());
				positionComma = Utility.searchSeparationComma(string);
			}
			type.termList.add(Term.newInstance(string));
		}
		return type;
	}
	
	private Type(){}

	public Type(LinkedList<Adjective> adjectiveList, ItemId itemId, LinkedList<Term> termList, boolean withType) throws DliException {
		if (adjectiveList == null || itemId == null || termList == null){
			throw new DliException();
		}
		this.adjectiveList = adjectiveList;
		this.itemId = itemId;
		this.termList = termList;
		this.withType = withType;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		if (withType) sb.append("$type(");
		for (Adjective adjective : adjectiveList){
			sb.append(adjective.toString());
			sb.append(",");
		}
		sb.append(itemId.toString());
		if (termList.size()>0){
			sb.append("(");
			boolean first = true;
			for (Term term : termList){
				if (first) first=false;
				else sb.append(",");
				sb.append(term.toString());
			}
			sb.append(")");
		}
		if (withType) sb.append(")");

		return sb.toString();
	}

	public LinkedList<Adjective> getAdjectiveList() {
		return adjectiveList;
	}

	public ItemId getItemId() {
		return itemId;
	}

	public LinkedList<Term> getTermList() {
		return termList;
	}

	public boolean isWithType() {
		return withType;
	}
	
}
